package com.vancouver.knows.ui.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.google.android.gms.analytics.CampaignTrackingReceiver;


public class Tracker extends BroadcastReceiver {
                  
	private String referrer = null;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		if("com.android.vending.INSTALL_REFERRER".equals(intent.getAction())){
			new CampaignTrackingReceiver().onReceive(context, intent);
			new com.appsflyer.AppsFlyerLib().onReceive(context, intent);


//			String referer = null;
//			try {
//				Intent gpIntent = new Intent(intent);
//				referer = URLDecoder.decode(intent.getStringExtra("referrer"),"UTF-8");
//				gpIntent.putExtra("referrer", referer);
//				new CampaignTrackingReceiver().onReceive(context, gpIntent);
//			} catch (UnsupportedEncodingException e1) {
//				new CampaignTrackingReceiver().onReceive(context, intent);
//				referer = null;
//			}
//
//			reportRef(intent, context);
		}
	}
	
	private  void reportRef(Intent intent ,Context context){
		if(intent == null){
			return ;
		}
		try {
			referrer = URLDecoder.decode(intent.getStringExtra("referrer"),"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			referrer = null;
		}
		if(referrer == null || referrer.trim().equals("")) {
			receiverAppsFlyerLib(context, intent);
			return;
		}

		//appsflyer 追踪
		if (referrer.contains("af_tranid")){
			receiverAppsFlyerLib(context, intent);
			return;
		}


		receiverAppsFlyerLib(context, intent);
	}

	private void receiverAppsFlyerLib(Context context, Intent intent){
		new com.appsflyer.AppsFlyerLib().onReceive(context, intent);
	}
}
